package cn.new7as.cas5s.ui.repay;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.List;

import cn.new7as.cas5s.ui.easy.netapi.PayUrlApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.databinding.ActivityOrdersDetailBinding;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.PayChannelApi;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.easy.netapi.OrderContentApi;
import cn.new7as.cas5s.ui.apply.ChangeForthActivity;
import cn.new7as.cas5s.ui.apply.EditForthActivity;


public class OrdersDetailActivity extends BaseActivity {


    private ActivityOrdersDetailBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOrdersDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("");
        initV();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isedit) {

            isedit = false;
        }
    }

    boolean isedit = true;

    void setLinerarHeight(LinearLayout mLinearLayout, int height) {
        ViewGroup.LayoutParams lp;
        lp = mLinearLayout.getLayoutParams();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = height;
        mLinearLayout.setLayoutParams(lp);
    }

    void dosd(OrderApi.Data data) {
        int i = Utils.getordStaus(data);
        int did = 0;

        if (i == 1) {
            binding.osta.setText("Overdue for " + data.overdueDays + " days");

            did = R.mipmap.iconstop;
            binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
            binding.llsettle.setVisibility(View.VISIBLE);
            binding.loverdue.setVisibility(View.VISIBLE);
            binding.llpaycontent.setVisibility(View.VISIBLE);
            getChanel(data);
            binding.osta.setBackgroundResource(R.drawable.overdueo);

        } else if (i == 2) {
            binding.osta.setText("Repayment Due in " + data.expireDays + " Days");

            did = R.mipmap.iconpay;
            binding.tvloanamoutsname.setText("Repeyment Amount (₹)");
            binding.llsettle.setVisibility(View.VISIBLE);
            binding.llpaycontent.setVisibility(View.VISIBLE);
            getChanel(data);
            binding.osta.setBackgroundResource(R.drawable.payo);
        } else if (i == 3) {
            binding.llorderd.measure(0, 0);
            int height = binding.llorderd.getMeasuredHeight();
            Log.e("aaaa", height + "");
            setLinerarHeight(binding.llorderd, height - 400);
            binding.osta.setText("Pending");
            binding.llpaycontent.setVisibility(View.GONE);

            did = R.mipmap.iconpend;
            binding.llsettlebefor.setVisibility(View.VISIBLE);
            binding.tvloanterms.setVisibility(View.VISIBLE);
            binding.llploandetail.setVisibility(View.GONE);
            binding.loanterm.setText(data.period.replace("Days", ""));
            binding.osta.setBackgroundResource(R.drawable.waito);
            if (data.orderStatus ==0)
            binding.editba.setVisibility(View.VISIBLE);
            binding.editba.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.go(OrdersDetailActivity.this, EditForthActivity.class);
                    isedit = true;
                }
            });
        } else if (i == 4) {
            binding.osta.setText("Rejected");

            did = R.mipmap.iconrefuse;
            binding.llpaycontent.setVisibility(View.GONE);
            binding.llorderd.setBackgroundResource(R.mipmap.orderbacktop);
            binding.osta.setBackgroundResource(R.drawable.refuseo);
            binding.lltop.setVisibility(View.GONE);
        } else if (i == 5) {
            binding.osta.setText("Paid off");
            binding.osta.setBackgroundResource(R.drawable.doneo);
            binding.llsettledate.setVisibility(View.VISIBLE);
            binding.llpaycontent.setVisibility(View.GONE);
            binding.lltop.setVisibility(View.GONE);

            did = R.mipmap.icondone;
        } else if (i == 6) {
            binding.osta.setVisibility(View.GONE);
            binding.llorderd.measure(0, 0);
            int height = binding.llorderd.getMeasuredHeight();

            setLinerarHeight(binding.llorderd, height - 350);
            binding.osta.setText("Pending");
            binding.llpaycontent.setVisibility(View.GONE);
            setop("Payment Failed");
            did = R.mipmap.iconpend;
            binding.llsettlebefor.setVisibility(View.VISIBLE);
            binding.tvloanterms.setVisibility(View.VISIBLE);
            binding.llploandetail.setVisibility(View.GONE);
            binding.loanterm.setText(data.period.replace("Days", ""));
            binding.osta.setBackgroundResource(R.drawable.waito);
            binding.verifyme.setVisibility(View.VISIBLE);
            binding.verifyme.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Utils.go(OrdersDetailActivity.this, ChangeForthActivity.class);
                }
            });
        }

        binding.osta.setCompoundDrawablesWithIntrinsicBounds(did, 0, 0, 0);


        binding.settlementamount.setText(data.repaymentAmount.replace("₹", ""));
        binding.extendamount.setText(data.extensionRepaymentAmount.replace("₹", ""));
        binding.availableloanamount.setText(data.orderAmount.replace("₹", ""));
        binding.loanamount.setText(data.orderAmount.replace("₹", ""));
        binding.loadperiod.setText(data.period.replace("Days", ""));

        binding.interest.setText(data.interest + "");
        binding.serviceCharge.setText(data.totalServiceFee + "");
        binding.interest1.setText(data.interest + "");
        binding.serviceCharge1.setText(data.totalServiceFee + "");
        binding.ifscno.setText(data.ifsc);
        binding.accountno.setText(data.cardNo);
        binding.upi.setText(data.upi);
        binding.overdueAb.setText(data.totalOverdueFee.replace("₹", ""));
        binding.settlementdateAb.setText(data.actualRepayMentDate);
        binding.disbursementamountAb.setText(data.receivedTotalCapital.replace("₹", ""));
        binding.repaymentamountAb.setText(data.predictTotalCapital.replace("₹", ""));
        try {
            binding.scheduledrepaymentdateAb.setText(data.repayMentDate.substring(0, 10));
        } catch (Exception e) {

        }
        binding.channels.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showbottom();
            }
        });
        binding.settle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPay(1, data);
            }
        });
        binding.extend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPay(2, data);
            }
        });
        binding.subproof.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.goextra(OrdersDetailActivity.this, SubProofActivity.class, data.tradeNo);
            }
        });
    }

    @Override
    public void initV() {
        super.initV();
        OrderApi.Data data = (OrderApi.Data) getIntent().getExtras().getSerializable("ddba");
        if (data.fo == 0) getorderdeCOntent(data.productCategorySubCode);
        else {
            dosd(data);
        }
    }

    int sele = 0;

    void showbottom() {
        new XPopup.Builder(this)
                .isDestroyOnDismiss(true)

                .asBottomList("Repayment Channels", strings,
                        null, -1,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                sele = position;
                                binding.channels.setText(strings[sele]);
                            }
                        })
                .show();
    }

    private void getorderdeCOntent(String code) {
        show();
        EasyHttp.post(this).api(new OrderContentApi(getunique(), code)).request(new OnHttpListener<HttpData<OrderContentApi.OrderContentData>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<OrderContentApi.OrderContentData> result) {
                hide();
                try {
                    OrderApi.Data data = result.getResult().repaymentRecord;
                    data.fo = 0;
                    dosd(data);
                } catch (Exception e) {
                    toa(getString(R.string.http_data_wrong));
                }

            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }

    private String[] strings;
    List<PayChannelApi.Data> result1;

    private void getChanel(OrderApi.Data data) {
        EasyHttp.get(this).api(new PayChannelApi(getunique(), data.tradeNo)).request(new OnHttpListener<HttpData<List<PayChannelApi.Data>>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<List<PayChannelApi.Data>> result) {
                result1 = (List<PayChannelApi.Data>) result.getResult();
                if (!result1.isEmpty()) {

                    strings = new String[result1.size()];
                    for (int i = 0; i < result1.size(); i++) {
                        strings[i] = "Options" + (i + 1);
                    }
                    binding.channels.setText(strings[0]);
                }
            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }


    private void getPay(int type, OrderApi.Data data) {
        show();
        EasyHttp.post(this).api(new PayUrlApi(getunique(), result1.get(sele).code, type, data.tradeNo)).request(new OnHttpListener<HttpData<String>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<String> result) {
                Utils.goextra(OrdersDetailActivity.this, PayForthActivity.class, result.getResult());
                hide();
            }


            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }
}